import { Component, OnInit,ViewChild } from '@angular/core';
import {ModalDirective} from 'ngx-bootstrap';
import {LocalStorageService} from '../../../lib/service/local-storage.service';
import {DeliveryManageService} from './delivery-manage.service';

@Component({
  selector: 'app-delivery-manage',
  templateUrl: './delivery-manage.component.html',
  styleUrls: ['./delivery-manage.component.scss'],
  providers: [DeliveryManageService]
})
export class DeliveryManageComponent implements OnInit {

  token: any;
  type: string;
  message: string;
  isShow = false;

  operateId: number;  // 操作的id
  expressTempList = ''; // 运费模板列表
  expressTempDetail = ''; // 运费模板内容详情

  @ViewChild('deleteModal') public deleteModal: ModalDirective;
  constructor(private localStorageService: LocalStorageService,
              private deliveryManageService: DeliveryManageService) { }

  ngOnInit() {
    this.token = this.localStorageService.getToken();
    this.getExpressTempList();
  }

  // 获取运费模板列表
  getExpressTempList(){
    let applet_uuid = this.localStorageService.getNowApplet().applet_uuid;
    this.deliveryManageService.getExpressTempList(applet_uuid)
      .subscribe(
        res => {
          this.expressTempList = res.data.data;
        }
      )
  }

  // 查看运费模板内容详情
  showDetail(id: number){
    this.operateId = id;
    this.deliveryManageService.getExpressTempValList(this.operateId)
      .subscribe(
        res => {
          this.expressTempDetail = res.data.data;
        }
      )
  }

  // 删除模板
  delTemp(id: number){
    this.operateId = id;
    this.deleteModal.show();
  }
  delSubmit(){
    this.deliveryManageService.delExpressTemp(this.operateId)
      .subscribe(
        res => {
          if(res.code === 2000){
            this.type = 'success';
            this.message = '删除成功';
            this.isShow = true;
            this.deleteModal.hide();
            this.getExpressTempList();
          }else{
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }

  promptIsShow(event: any): void{
    this.isShow = event;
  }
}
